package com.vip.jwt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vip.jwt.entity.Role;
import com.vip.jwt.entity.UserRole;
import com.vip.jwt.mapper.RoleMapper;
import com.vip.jwt.mapper.UserRoleMapper;
import com.vip.jwt.service.UserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户角色关联表 服务实现类
 * </p>
 *
 * @author vip
 * @since 2021-05-04
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private RoleMapper roleMapper;

    @Override
    public int deleteByUserId(String userId) {
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.eq("user_id",userId);
        return userRoleMapper.delete(userRoleQueryWrapper);
    }

    @Override
    public List<Role> findByUserId(String userId) {
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.eq("user_id",userId);
        List<UserRole> userRoles = userRoleMapper.selectList(userRoleQueryWrapper);
        List<Role> roles = new ArrayList<>();
        for(int i=0;i<userRoles.size();i++){
            UserRole userRole = userRoles.get(i);
            String roleId = userRole.getRoleId();
            Role role = roleMapper.selectById(roleId);
            roles.add(role);
        }
        return roles;
    }

}
